home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9866 / 9866.xpi / modules / samfind_modaffiliate.jsm < prev    next >
Encoding:
Text File  |  2009-12-08  |  7.4 KB  |  275 lines

  1. //-------------------------------------------------------------------------------------------
  2. // Affiliate module.
  3. //-------------------------------------------------------------------------------------------
  4.  
  5. const Cc = Components.classes;
  6. const Ci = Components.interfaces;
  7.  
  8. var EXPORTED_SYMBOLS = ["samfind_modaffiliate"];
  9.  
  10. Components.utils.import("resource://samfind/samfind_modutils.jsm");
  11.  
  12. var samfind_modaffiliate =
  13. {
  14.     _base_urls : null,
  15.     _styles : null,
  16.     _latest_base_url : null,
  17.     _latest_base_url_counter : 0,
  18.  
  19.     _init : function()
  20.     {
  21.         var file = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("ProfD", Ci.nsIFile);
  22.         file.append("samfindbaseurls.xml");
  23.         if (file.exists())
  24.         {
  25.             var dom = samfind_modutils.loadXML(file);
  26.             samfind_modaffiliate._base_urls = dom.getElementsByTagName("baseURL");
  27.             samfind_modaffiliate.getStyles(dom);
  28.             dom = null;
  29.         }
  30.         else
  31.         {
  32.             samfind_modutils._pref_branch.setCharPref("affiliates.sha1", "0");    
  33.         }
  34.         file = null;
  35.         //
  36.         samfind_modaffiliate.fetchBaseURLs();
  37.     },
  38.  
  39.     fetchBaseURLs : function()
  40.     {
  41.         try
  42.         { 
  43.             var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
  44.             xhr.open("POST", "http://samfind.com/XML/affiliates.php", true);
  45.             xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
  46.             xhr.setRequestHeader("Accept-Encoding", "gzip");
  47.             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  48.             xhr.setRequestHeader("User-Agent", "samfind Toolbar");
  49.             var timeout = samfind_modutils.setXhrAbortTimeout(xhr, 10000);
  50.             xhr.onreadystatechange = function(e)
  51.             {
  52.                 if (xhr.readyState == 4)
  53.                 {
  54.                     timeout.cancel();
  55.                     timeout = null;
  56.                     var retry = true;
  57.                     try
  58.                     {
  59.                         if (xhr.status == 200)
  60.                         {
  61.                             var dom_parser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);
  62.                             var dom = dom_parser.parseFromString(xhr.responseText, "text/xml");
  63.                             dom_parser = null;
  64.                             var document_element = dom.documentElement;
  65.                             if (document_element.nodeName != "parsererror")
  66.                             {
  67.                                 retry = false;
  68.                                 if (document_element.childNodes.length)
  69.                                 {
  70.                                     samfind_modutils._pref_branch.setCharPref("affiliates.sha1", xhr.getResponseHeader("X-samfind-sha1"));
  71.                                     samfind_modutils.saveXML("samfindbaseurls.xml", dom);
  72.                                     samfind_modaffiliate._base_urls = document_element.getElementsByTagName("baseURL");
  73.                                     samfind_modaffiliate.getStyles(document_element);
  74.                                 }
  75.                             }
  76.                             document_element = null;
  77.                             dom = null;
  78.                         }
  79.                         else if (xhr.status == 304)
  80.                         {
  81.                             retry = false;
  82.                         }
  83.                     }
  84.                     catch (e)
  85.                     {}
  86.                     xhr = null;
  87.                     if (retry)
  88.                     {
  89.                         samfind_modaffiliate.retryFetchBaseURLs();
  90.                     }
  91.                 }
  92.             };
  93.             xhr.send("sha1=" + samfind_modutils._pref_branch.getCharPref("affiliates.sha1"));
  94.         }
  95.         catch (e)
  96.         {
  97.             samfind_modaffiliate.retryFetchBaseURLs();
  98.         }
  99.     },
  100.  
  101.     retryFetchBaseURLs : function()
  102.     {
  103.         var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  104.         timer.initWithCallback({ notify : function() { samfind_modaffiliate.fetchBaseURLs(); } }, 30000, Ci.nsITimer.TYPE_ONE_SHOT);
  105.     },
  106.  
  107.     getStyles : function(dom)
  108.     {
  109.         samfind_modaffiliate._styles = [];
  110.         try
  111.         {        
  112.             var styles = dom.getElementsByTagName("style");
  113.             for (var i=0; i<styles.length; ++i)
  114.             {
  115.                 var style = styles[i];
  116.                 var engine = style.getAttribute("engine");
  117.                 var value = style.getAttribute("value");
  118.                 if (engine != null && engine.length && value != null && value.length)
  119.                 {
  120.                     samfind_modaffiliate._styles[engine] = value;
  121.                 }
  122.             }
  123.         }
  124.         catch (e)
  125.         {}
  126.     },
  127.  
  128.     checkAffiliateBaseURL : function(url)
  129.     {
  130.         if (!samfind_modaffiliate._base_urls)
  131.         {
  132.             return;
  133.         }
  134.         try
  135.         {
  136.             var url_object = samfind_modutils._io_service.newURI(url, null, null);
  137.             var url_host = url_object.host;
  138.             if (url_host.indexOf("www.") == 0)
  139.             {
  140.                 url_host = url_host.substring(4);
  141.             }
  142.             var url_host_and_path = url_host + decodeURIComponent(url_object.path);
  143.             for (var i=0; i<samfind_modaffiliate._base_urls.length; ++i)
  144.             {
  145.                 var base_url = samfind_modaffiliate._base_urls[i].getAttribute("v");
  146.                 if (url_host_and_path.indexOf(base_url) != -1)
  147.                 {
  148.                     if (base_url == samfind_modaffiliate._latest_base_url)
  149.                     {
  150.                         if (++(samfind_modaffiliate._latest_base_url_counter) == 10)
  151.                         {
  152.                             samfind_modaffiliate._latest_base_url_counter = 0;
  153.                             samfind_modaffiliate.fetchAffiliateURL();    
  154.                         }
  155.                     }
  156.                     else
  157.                     {
  158.                         samfind_modaffiliate._latest_base_url = base_url;
  159.                         samfind_modaffiliate._latest_base_url_counter = 0;
  160.                         samfind_modaffiliate.fetchAffiliateURL();
  161.                     }
  162.                     break;
  163.                 }
  164.             }
  165.             url_host_and_path = null;
  166.             url_object = null;
  167.         }
  168.         catch (e)
  169.         {}
  170.     },
  171.  
  172.     fetchAffiliateURL : function()
  173.     {
  174.         try
  175.         {
  176.             var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
  177.             xhr.open("POST", "http://samfind.com/XML/affiliates.php", true);
  178.             xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
  179.             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  180.             xhr.setRequestHeader("User-Agent", "samfind Toolbar");
  181.             var timeout = samfind_modutils.setXhrAbortTimeout(xhr, 10000);
  182.             xhr.onreadystatechange = function(e)
  183.             {
  184.                 if (xhr.readyState == 4)
  185.                 {
  186.                     timeout.cancel();
  187.                     timeout = null;
  188.                     //
  189.                     try
  190.                     {
  191.                         if (xhr.status == 200)
  192.                         {
  193.                             samfind_modutils.loadIframe(xhr.responseText, null);
  194.                         }
  195.                     }
  196.                     catch (e)
  197.                     {}
  198.                     //
  199.                     xhr = null;
  200.                 }
  201.             };
  202.             xhr.send("baseURL=" + samfind_modaffiliate._latest_base_url);    
  203.         }
  204.         catch (e)
  205.         {}
  206.     },
  207.  
  208.     //-------------------------------------------------------------------------------------------
  209.     //
  210.     //-------------------------------------------------------------------------------------------
  211.  
  212.     getElements : function(engine, doc, xpath)
  213.     {
  214.         var response = new Array();
  215.         if (samfind_modaffiliate._base_urls && samfind_modaffiliate._styles != null && samfind_modaffiliate._styles[engine] != null)
  216.         {
  217.             var elements = doc.evaluate(xpath, doc, null, 0, null);
  218.             var element = elements.iterateNext();    
  219.             while (element)
  220.             {
  221.                 response.push(element);
  222.                 element = elements.iterateNext();
  223.             }
  224.         }
  225.         return response;
  226.     },
  227.  
  228.     elementIsAffiliate : function(url)
  229.     {
  230.         try
  231.         {
  232.             var url_object = samfind_modutils._io_service.newURI(url, null, null);
  233.             var url_host = url_object.host;
  234.             if (url_host.indexOf("www.") == 0)
  235.             {
  236.                 url_host = url_host.substring(4);
  237.             }
  238.             var url_host_and_path = url_host + decodeURIComponent(url_object.path);
  239.             for (var i=0; i<samfind_modaffiliate._base_urls.length; ++i)
  240.             {
  241.                 var base_url = samfind_modaffiliate._base_urls[i];
  242.                 var base_url_value = base_url.getAttribute("v");
  243.                 if (url_host_and_path.indexOf(base_url_value) == 0)
  244.                 {
  245.                     return base_url;
  246.                 }
  247.             }
  248.         }
  249.         catch (e)
  250.         {}
  251.         return null;
  252.     },
  253.  
  254.     highlightElement : function(engine, element)
  255.     {
  256.         var style = samfind_modaffiliate._styles[engine];
  257.         if (arguments.length == 2)
  258.         {
  259.             element.setAttribute("style", style);
  260.         }
  261.         else
  262.         {
  263.             var sub_elements = arguments[2];
  264.             for (var j=0; j<sub_elements.length; ++j)
  265.             {
  266.                 sub_elements[j].setAttribute("style", style);
  267.             }
  268.         }
  269.     }
  270. };
  271.  
  272. /**
  273.  * Constructor.
  274.  */
  275. (function() { this._init(); }).apply(samfind_modaffiliate);